  var count11=0;
  var count12=0;
  var count13=0;
  var count14=0;

  var count21=0;
  var count22=0;
  var count23=0;
  var count24=0;
  var count25=0;
  var count26=0;
  var count27=0;

  var count31=0;
  var count32=0;
  var count33=0;
  var count34=0;
  var count35=0;
  var count36=0;

  var count41=0;
  var count42=0;
  var count43=0;
  var count44=0;
  var count45=0;
  var count46=0;

  var count51=0;
  var count52=0;
  var count53=0;
  var count54=0;
  var count55=0;
  var count56=0;


//-----------------------------------------------------------------------------
  function display_table(tab_no,order_by){
    var loading = document.getElementById("loading");
    loading.style.display = "block";

    switch(tab_no){
      case 1:url="../wp-content/plugins/course_management/display_courses.php";break;
      case 2:url="../wp-content/plugins/course_management/display_lectures.php";break;
      case 3:url="../wp-content/plugins/course_management/display_exercises.php";break;
      case 4:url="display_novel.php";break;
      case 5:url="display_edification.php";break;
    }
    var obj = document.getElementById("tab");
			$.post(url, {"order_by":order_by},function(data) {
				$('#tab').html(data);
				loading.style.display = "none";
			});

  }
//-----------------------------------------------------------------------------
  function add_course(oper,rec){
			var loading = document.getElementById("loading");
			loading.style.display = "block";

			var url = "../wp-content/plugins/course_management/add_course.php";

			$.post(url, {"oper":oper, "rec":rec},function(data) {
				$('#tab').html(data);
				loading.style.display = "none";
			});
  }
//-----------------------------------------------------------------------------
  function copy_course(){
			var url = "../wp-content/plugins/course_management/copy_course.php";
            var course_id = document.getElementById("copy_course").options[document.getElementById("copy_course").selectedIndex].value;
            if(course_id==0){
              alert("Before copying choose existing course!");
              return false;
            }
			$.post(url, {"course_id":course_id},function(data) {
				$('#tab').html(data);
			});
  }
//-----------------------------------------------------------------------------
  function add_lecture(oper,rec){
			var loading = document.getElementById("loading");
			loading.style.display = "block";
			
    	    var course_id = document.getElementById("course").options[document.getElementById("course").selectedIndex].value;

			var url = "../wp-content/plugins/course_management/add_lecture.php";

			$.post(url, {"oper":oper, "rec":rec, "course_id":course_id},function(data) {
				$('#tab').html(data);
				loading.style.display = "none";
			});
  }
//-----------------------------------------------------------------------------
  function add_exercise(oper,rec){
    	    var course_id = document.getElementById("course").options[document.getElementById("course").selectedIndex].value;

			var url = "../wp-content/plugins/course_management/add_exercise.php";

			$.post(url, {"oper":oper, "rec":rec, "course_id":course_id},function(data) {
				$('#tab').html(data);
			});
  }
//-----------------------------------------------------------------------------
  function save_course(oper, rec){
   	var name = document.getElementById("name").value;
    if(name.length==0){
      alert("The name can not be empty!");
      return false;
    }
    var year = document.getElementById("year").value;
    if(year.length==0){
      alert("The year can not be empty!");
      return false;
    }
    /*var semester = document.getElementById("semester").value;
    if(semester.length==0){
      alert("The semester can not be empty!");
      return false;
    }*/
    var semester_value = document.getElementById("semester").options[document.getElementById("semester").selectedIndex].value;
    if(semester_value==1)
        semester="Spring";
    else
      if(semester_value==2)
        semester="Fall";
      else{
        alert("The semester can not be empty!");
        return false;
      }
    var content = document.getElementById("content").value;
    if(content.length==0){
      alert("The content can not be empty!");
      return false;
    }
    var instCount = $('#tblInstructor tr').length;
    if(instCount<=0)
    {
    	alert("Add atleast one staff!");
        return false;
    }
    
	var course = new Object();
	course.oper = oper;
	course.rec = rec;
	course.name = name;
	course.year = year;
	course.semester = semester;
	course.content = content;
	course.instrustors = getInstructors();
	var dataString = JSON.stringify(course);
			var loading = document.getElementById("loading");
			loading.style.display = "block";

			var url = "../wp-content/plugins/course_management/save_course.php";

	$.post(url, {
		data : dataString
	}, function(res) {
		var obj = JSON.parse(res);
		var id = obj.id;
		//$('#tab').html(obj.message);
		loading.style.display = "none";
		var url = "../wp-content/plugins/course_management/display_courses.php";
		$.get(url, function(data) {
			  $('#tab').html(obj.message + data);
			  //alert('Load was performed.');
			});
	});
  }
//-----------------------------------------------------------------------------
  function save_copy_course(course_id){
    var starting_date = document.getElementById("starting_date").value;
    if(starting_date.length==0){
      alert("The starting_date can not be empty!");
      return false;
    }
    if(checkdate(starting_date)==true){
			var url = "../wp-content/plugins/course_management/save_copy_course.php";

			$.post(url, {"course_id":course_id,"starting_date":starting_date},function(data) {
				$('#tab').html(data);
			});
	}
  }
//-----------------------------------------------------------------------------
  function save_lecture(oper, rec){
    var course_id = document.getElementById("course_select").options[document.getElementById("course_select").selectedIndex].value;
    if(course_id==0){
        alert("The course name can not be empty!");
        return false;
    }

   	var seq_num = document.getElementById("seq_num").value;
    if(seq_num.length==0){
      alert("The sequence number can not be empty!");
      return false;
    }
    var date = document.getElementById("date").value;
    if(date.length==0){
      alert("The date can not be empty!");
      return false;
    }
    
    if(checkdate(date)==false){
        return false;
    }

    var description = document.getElementById("description").value;
    if(description.length==0){
      alert("The description can not be empty!");
      return false;
    }
    /*var slide = document.getElementById("slide").value;
    if(slide.length==0){
      alert("The slide can not be empty!");
      return false;
    }*/
    //alert(slide);
    /*var additional = document.getElementById("additional").value;
    if(additional.length==0){
      //alert("The additional can not be empty!");
      //return false;
    }*/

			var loading = document.getElementById("loading");
			loading.style.display = "block";

			var url = "../wp-content/plugins/course_management/save_lecture.php";

			$.post(url, {"course_id":course_id,"seq_num":seq_num,"date":date,"description":description,"oper":oper,"rec":rec},function(data) {
				$('#tab').html(data);
				loading.style.display = "none";
			});
  }
//-----------------------------------------------------------------------------
  function save_exercise(oper, rec){
    var course_id = document.getElementById("course_select").options[document.getElementById("course_select").selectedIndex].value;
    if(course_id==0){
        alert("The course name can not be empty!");
        return false;
    }

   	var seq_num = document.getElementById("seq_num").value;
    if(seq_num.length==0){
      alert("The sequence number can not be empty!");
      return false;
    }
    var date = document.getElementById("date").value;
    if(date.length==0){
      alert("The date can not be empty!");
      return false;
    }
    if(checkdate(date)==false){
        return false;
    }
    var description = document.getElementById("description").value;
    if(description.length==0){
      alert("The description can not be empty!");
      return false;
    }
    var deadline = document.getElementById("deadline").value;
    if(deadline.length==0){
      alert("The deadline can not be empty!");
      return false;
    }

			var url = "../wp-content/plugins/course_management/save_exercise.php";

			$.post(url, {"course_id":course_id,"seq_num":seq_num,"date":date,"description":description,"deadline":deadline,"oper":oper,"rec":rec},function(data) {
				$('#tab').html(data);
			});
  }


//-----------------------------------------------------------------------------
  function delete_course(id){
    var url = "../wp-content/plugins/course_management/delete_course.php";

    if(!confirm('Are you sure to delete this record?')) return false;
    else{
			$.post(url, {"id":id},function(data) {
				$('#tab').html(data);
			});
			
    }
  }
//-----------------------------------------------------------------------------
  function delete_lecture(id){
    var url = "../wp-content/plugins/course_management/delete_lecture.php";
    var course_id = document.getElementById("course").options[document.getElementById("course").selectedIndex].value;
    if(!confirm('Are you sure to delete this record?')) return false;
    else{
			$.post(url, {"id":id,"course_id":course_id},function(data) {
				$('#tab').html(data);
			});

    }
  }
//-----------------------------------------------------------------------------
  function delete_exercise(id){
    var url = "../wp-content/plugins/course_management/delete_exercise.php";
    var course_id = document.getElementById("course").options[document.getElementById("course").selectedIndex].value;
    if(!confirm('Are you sure to delete this record?')) return false;
    else{
			$.post(url, {"id":id,"course_id":course_id},function(data) {
				$('#tab').html(data);
			});

    }
  }
//-----------------------------------------------------------------------------
function c_l_change(th){
    var course_id = th.options[th.selectedIndex].value;

    var loading = document.getElementById("loading");
    loading.style.display = "block";

    url="../wp-content/plugins/course_management/display_lectures.php";
    var obj = document.getElementById("tab");
			$.post(url, {"course_id":course_id},function(data) {
				$('#tab').html(data);
				loading.style.display = "none";
			});
}
//-----------------------------------------------------------------------------
function c_e_change(th){
    var course_id = th.options[th.selectedIndex].value;

    var loading = document.getElementById("loading");
    loading.style.display = "block";

    url="../wp-content/plugins/course_management/display_exercises.php";
    var obj = document.getElementById("tab");
			$.post(url, {"course_id":course_id},function(data) {
				$('#tab').html(data);
				loading.style.display = "none";
			});
}
//-----------------------------------------------------------------------------
function c_c_change(th){
    var course_name = th.options[th.selectedIndex].value;

    url="../wp-content/plugins/course_management/display_courses.php";
    var obj = document.getElementById("tab");
			$.post(url, {"course_name":course_name},function(data) {
				$('#tab').html(data);
			});
}
//-----------------------------------------------------------------------------
function lecture_slide(lecture_id){
    var course_id = document.getElementById("course").options[document.getElementById("course").selectedIndex].value;
    var loading = document.getElementById("loading");
    loading.style.display = "block";

    url="../wp-content/plugins/course_management/save_lecture_slide.php";
    var obj = document.getElementById("tab");
			$.post(url, {"lecture_id":lecture_id,"course_id":course_id},function(data) {
				$('#tab').html(data);
				loading.style.display = "none";
			});
}
//-----------------------------------------------------------------------------
function exercise_slide(exercise_id){
    var course_id = document.getElementById("course").options[document.getElementById("course").selectedIndex].value;

    url="../wp-content/plugins/course_management/save_exercise_slide.php";
    var obj = document.getElementById("tab");
			$.post(url, {"exercise_id":exercise_id,"course_id":course_id},function(data) {
				$('#tab').html(data);
			});
}
//-----------------------------------------------------------------------------
function lecture_additional(lecture_id){
    var course_id = document.getElementById("course").options[document.getElementById("course").selectedIndex].value;
    var loading = document.getElementById("loading");
    loading.style.display = "block";

    url="../wp-content/plugins/course_management/save_lecture_additional.php";
    var obj = document.getElementById("tab");
			$.post(url, {"lecture_id":lecture_id,"course_id":course_id},function(data) {
				$('#tab').html(data);
				loading.style.display = "none";
			});
}
//-----------------------------------------------------------------------------
function exercise_additional(exercise_id){
    var course_id = document.getElementById("course").options[document.getElementById("course").selectedIndex].value;

    url="../wp-content/plugins/course_management/save_exercise_additional.php";
    var obj = document.getElementById("tab");
			$.post(url, {"exercise_id":exercise_id,"course_id":course_id},function(data) {
				$('#tab').html(data);
			});
}
//-----------------------------------------------------------------------------
function exercise_file(exercise_id){
    var course_id = document.getElementById("course").options[document.getElementById("course").selectedIndex].value;

    url="../wp-content/plugins/course_management/save_exercise_file.php";
    var obj = document.getElementById("tab");
			$.post(url, {"exercise_id":exercise_id,"course_id":course_id},function(data) {
				$('#tab').html(data);
			});
}
//-----------------------------------------------------------------------------
function exercise_solution(exercise_id){
    var course_id = document.getElementById("course").options[document.getElementById("course").selectedIndex].value;

    url="../wp-content/plugins/course_management/save_exercise_solution.php";
    var obj = document.getElementById("tab");
			$.post(url, {"exercise_id":exercise_id,"course_id":course_id},function(data) {
				$('#tab').html(data);
			});
}

//-----------------------------------------------------------------------------
    function course_click(course_id){
        return ;
        //alert(url);
        alert(course_id);
        //window.location.href = url;
        var i;
        for(i=1; i<5; i++){
            //alert(i);
            //alert(document.getElementById("course"+i));
            if(document.getElementById("course"+i)){
                document.getElementById("course"+i).style.display = "none";
                //alert("kirdim");
            }
        }
        document.getElementById("course"+course_id).style.display = "block";

    }


    
/**--------------------------
//* Validate Date Field script- By JavaScriptKit.com
//* For this script and 100s more, visit http://www.javascriptkit.com
//* This notice must stay intact for usage
---------------------------**/

function checkdate(input){
  var validformat=/^\d{4}\-\d{2}\-\d{2}$/ //Basic check for format validity
  //var returnval=false
  if (!validformat.test(input)){
    alert("Invalid Date Format. Please correct and submit again.");7
    return false;
  }
  //else{ //Detailed check for valid date ranges
    //var monthfield=input.value.split("/")[0]
    //var dayfield=input.value.split("/")[1]
    //var yearfield=input.value.split("/")[2]
    //var dayobj = new Date(yearfield, monthfield-1, dayfield)
    //if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
    //alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
    //else
    //returnval=true
  //}
  return true;
}
function instructors_onChange() {
	if ($("#instructors option:selected").val() > 0)
		$("#addInstructor").removeAttr("disabled");
	else
		$("#addInstructor").attr("disabled", "disabled");
}

function addInstructor() {

	
		var rowCount = $('#tblInstructor tr').length;
		if ($("#instructors option").length - 1 == rowCount) {
			$("#addInstructor").attr("disabled", "disabled");
		} else {
			var id = $("#instructors option:selected").val();
			var name = $("#instructors option:selected").text();
			// alert("id is " + id + "value is " + name);
			// $('#tblInstructor > tbody:last').append('<tr><td>' + id +
			// '</td><td>'
			// + name + '</td></tr>');
			$('#tblInstructor > tbody:last')
					.append(
							'<tr id="'
									+ id
									+ '"><td>'
									+ '<a href="#" onclick="removeInstructor('
									+ id
									+ ')" ><img border="0" alt="Delete" src="../wp-content/plugins/course_management/images/delete.png"/></a>'
									+ '</td><td>' + name + '</td></tr>');
	}
}

function deleteCourseStaff(courseId,staffId)
{
	var courseStaff = new Object();
	courseStaff.method = "deleteCourseStaff";
	courseStaff.courseId = courseId;
	courseStaff.staffId = staffId;
	var dataString = JSON.stringify(courseStaff);

	var loading = document.getElementById("loading");
	loading.style.display = "block";

	var url = "../wp-content/plugins/course_management/courseStaff.php";

	$.post(url, {
		data : dataString
	}, function(res) {
		var obj = JSON.parse(res);
		var id = obj.id;
		removeInstructor(id);
		loading.style.display = "none";		
	});
}

function addCourseStaff(courseId)
{
	var staffId = $("#instructors option:selected").val();
	var courseStaff = new Object();
	courseStaff.method = "addCourseStaff";
	courseStaff.courseId = courseId;
	courseStaff.staffId = staffId;
	var dataString = JSON.stringify(courseStaff);

	var loading = document.getElementById("loading");
	loading.style.display = "block";

	var url = "../wp-content/plugins/course_management/courseStaff.php";

	$.post(url, {
		data : dataString
	}, function(res) {
		var obj = JSON.parse(res);
		var id = obj.id;
		addInstructor();
		loading.style.display = "none";		
	});
}

function removeInstructor(id) {
	javascript: $("#" + id).remove();
	instructors_onChange();
}

function getInstructors() {
	var instructors = new Array();
	$("#tblInstructor tbody tr").each(function() {
		// var Id = $(this).find("td").eq(0).html();
		var Id = $(this).attr("id");
		// var Name = $(this).find("td").eq(1).html();
		instructors.push(Id);
		// alert("CustomerId: " + customerId +"Name: " + customerName);
	});
	return instructors;

}